package example.filter;

import android.graphics.Bitmap;
import android.graphics.Color;

public class ScreenFilter {
	
    private int getRed(Bitmap bmp, int x, int y) {
    	return Color.red(bmp.getPixel(x,y));
    }
    private int getBlu(Bitmap bmp, int x, int y) {
    	return Color.blue(bmp.getPixel(x,y));
    }
    private int getGre(Bitmap bmp, int x, int y) {
    	return Color.green(bmp.getPixel(x,y));
    }

    public Bitmap filterBlur(Bitmap bmp) {
		Bitmap dst = Bitmap.createBitmap(bmp.getWidth(), bmp.getHeight(),Bitmap.Config.ARGB_8888);  
		int width = bmp.getWidth();
		int height = bmp.getHeight();
		int initpixels[] = new int[width * height];  
		dst.setPixels(initpixels, 0, width, 0, 0, width, height);
		
		int dstpixels[] = new int[width * height];
		final int biasX = 2;
		final int biasY = 2;
		for(int i = biasX; i < width-biasX; i++) {
			for(int j = biasY; j < height-biasY; j++) {
				double r = 0.f;
				double g = 0.f;
				double b = 0.f;
				
				double org_r = getRed(bmp,i		,j);
				double org_g = getGre(bmp,i		,j);
				double org_b = getBlu(bmp,i		,j);
				
                r += getRed(bmp,i		,j);
                r += getRed(bmp,i+biasX	,j);
                r += getRed(bmp,i		,j+biasY);
                r += getRed(bmp,i+biasX	,j+biasY);
                
                g += getGre(bmp,i		,j);
                g += getGre(bmp,i+biasX	,j);
                g += getGre(bmp,i		,j+biasY);
                g += getGre(bmp,i+biasX	,j+biasY);
                
                b += getBlu(bmp,i  		,j);
                b += getBlu(bmp,i+biasX	,j);
                b += getBlu(bmp,i  		,j+biasY);
                b += getBlu(bmp,i+biasX	,j+biasY);
				
                if(i < (bmp.getWidth()/2)) {
                	dstpixels[i + j * width] = Color.rgb((int)org_r,(int)org_g,(int)org_b);
                } else {
					dstpixels[i + j * width] = Color.rgb(
							((int)(r*0.25f)),
							((int)(g*0.25f)),
							((int)(b*0.25f)));
                }
			}
		}
		dst.setPixels(dstpixels, 0, width, 0, 0, width, height); 
		
		return dst;		  
    }
    
    public Bitmap filterMozaik(Bitmap bmp) {
        int dot = 4;  

        for (int i=0;i<bmp.getWidth()/dot;i++) {  
            for (int j=0;j<bmp.getHeight()/dot;j++){  
                int color = bmp.getPixel(i, j);  
                  
                int rr = 0;  
                int gg = 0;  
                int bb = 0;  
                for (int k=0;k<dot;k++) {  
                    for (int l=0;l<dot;l++){  
                        int dotColor = bmp.getPixel(i*dot+k, j*dot+l);  
                        rr += Color.red(dotColor);  
                        gg += Color.green(dotColor);  
                        bb += Color.blue(dotColor);  
                    }  
                }  
                rr = rr/(dot*dot);  
                gg = gg/(dot*dot);  
                bb = bb/(dot*dot);  
                  
                for (int k=0;k<dot;k++) {  
                    for (int l=0;l<dot;l++){  
                        bmp.setPixel(i*dot+k, j*dot+l, Color.rgb(rr, gg, bb));  
                    }  
                }  
            }  
        } 
        return bmp;
    }
    
    public Bitmap filterEdge(Bitmap bmp) {
		Bitmap dst = Bitmap.createBitmap(bmp.getWidth(), bmp.getHeight(),Bitmap.Config.ARGB_8888);  
		int width = bmp.getWidth();
		int height = bmp.getHeight();
		int initpixels[] = new int[width * height];  
		dst.setPixels(initpixels, 0, width, 0, 0, width, height);
		
		int dstpixels[] = new int[width * height];
		final int biasX = 4;
		final int biasY = 4;
		for(int i = biasX; i < width-biasX; i++) {
			for(int j = biasY; j < height-biasY; j++) {
				double r = 0.f;
				double g = 0.f;
				double b = 0.f;
				
				double org_r = getRed(bmp,i		,j);
				double org_g = getGre(bmp,i		,j);
				double org_b = getBlu(bmp,i		,j);
				
                r += getRed(bmp,i		,j);
                r += getRed(bmp,i+biasX	,j);
                r += getRed(bmp,i		,j+biasY);
                r += getRed(bmp,i+biasX	,j+biasY);
                
                g += getGre(bmp,i		,j);
                g += getGre(bmp,i+biasX	,j);
                g += getGre(bmp,i		,j+biasY);
                g += getGre(bmp,i+biasX	,j+biasY);
                
                b += getBlu(bmp,i  		,j);
                b += getBlu(bmp,i+biasX	,j);
                b += getBlu(bmp,i  		,j+biasY);
                b += getBlu(bmp,i+biasX	,j+biasY);
				
                if(i < (bmp.getWidth()/2)) {
                	dstpixels[i + j * width] = Color.rgb((int)org_r,(int)org_g,(int)org_b);
                } else {
					dstpixels[i + j * width] = Color.rgb(
							((int)(r*0.25f)) - (int)(org_r),
							((int)(g*0.25f)) - (int)(org_g),
							((int)(b*0.25f)) - (int)(org_b));
                }
			}
		}
		dst.setPixels(dstpixels, 0, width, 0, 0, width, height); 
		
		return dst;		  
    }
    
    public Bitmap filterSepia(Bitmap bmp) {
    	final double sepia_r = 1.f;
    	final double sepia_g = 0.8f;
    	final double sepia_b = 0.6f;
    	
        for (int i=0;i<bmp.getWidth();i++) {  
            for (int j=0;j<bmp.getHeight();j++){                        	
            	double r = getRed(bmp,i,j);
            	double g = getGre(bmp,i,j);
            	double b = getBlu(bmp,i,j);
            	
                int col_r = (int)((r+g+b) * 0.3333f * sepia_r);
                int col_g = (int)((r+g+b) * 0.3333f * sepia_g);
                int col_b = (int)((r+g+b) * 0.3333f * sepia_b);
                
                if(i < (bmp.getWidth()/2)) {
                	bmp.setPixel(i, j, Color.rgb((int)r,(int)g,(int)b));
                } else {
                	bmp.setPixel(i, j, Color.rgb(col_r,col_g,col_b));
                }
            }
        }
        return bmp;
    }
    
    public Bitmap filterNega(Bitmap bmp) {
        for (int i=0;i<bmp.getWidth();i++) {  
            for (int j=0;j<bmp.getHeight();j++){                    
            	int color = bmp.getPixel(i, j); 
            	
                int r = Color.red(color);  
                int g = Color.green(color);  
                int b = Color.blue(color);
                
                int result_r = 0;
                int result_g = 0;
                int result_b = 0;
                
            	result_r = 255 - r;
            	result_g = 255 - g;
            	result_b = 255 - b;
            	
            	if(i < bmp.getWidth()/2) {
            		bmp.setPixel(i, j, Color.rgb(r,g,b));
            	} else {
            		bmp.setPixel(i, j, Color.rgb(result_r,result_g,result_b));
            	}
            }
        }
        return bmp;
    }
    
    public Bitmap filterMono(Bitmap bmp) {
        for (int i=0;i<bmp.getWidth();i++) {  
            for (int j=0;j<bmp.getHeight();j++){                    
            	int color = bmp.getPixel(i, j); 
            	
                int r = Color.red(color);  
                int g = Color.green(color);  
                int b = Color.blue(color);
                
                int result_r = 0;
                int result_g = 0;
                int result_b = 0;
                
            	result_r = (int)((r+g+b) * 0.333f);
            	result_g = (int)((r+g+b) * 0.333f);
            	result_b = (int)((r+g+b) * 0.333f);
            	
            	if(i < bmp.getWidth()/2) {
            		bmp.setPixel(i, j, Color.rgb(r,g,b));
            	} else {
            		bmp.setPixel(i, j, Color.rgb(result_r,result_g,result_b));
            	}
            }
        }
        return bmp;
    }
}
